/**
 * Created by ayu on 2017/6/29.
 */
const wechat = require ('wechat');
const request = require ('request');
const wechatAppConfig = require ('../../resources/WechatAppConfig');

module.exports.reply = wechat (wechatAppConfig.app, wechat.text (( message, req, res ) =>
{
    let [ userInputMessage = '', openid = '' ] = [ message.Content.trim (), message.FromUserName.trim () ];
    if (userInputMessage === 'hello')
    {
        let replyContent = '';
        if (openid === wechatAppConfig.openid)
        {
            replyContent = '欢迎你，管理员';
        }
        else
        {
            replyContent = '欢迎来到百视通微信公众号 - 开发测试版';
        }
        res.reply (replyContent);
    }
}).image (function ( message, req, res )
{
    //console.log(message);
    res.reply ('pic');
}).location (function ( message, req, res )
{
    //console.log(message);
    res.reply ('location');
}).voice (function ( message, req, res )
{
    //console.log(message);
    res.reply ('voice');
}).link (function ( message, req, res )
{
    //console.log(message);
    res.reply ('link');
}).event (( message, req, res ) =>
{
    if (message.Event === 'subscribe')
    {
        // 用户添加时候的消息
        res.reply ('欢迎关注公众号');
    }
    else if (message.Event === 'unsubscribe')
    {
        res.reply ('Bye!');
    }
    else if (message.Event === 'CLICK')
    {
        let eventKey = message.EventKey;
        switch (eventKey)
        {
            case 'notice' :
            {
                noticeController.queryNotice (function ( notices )
                {
                    let noticeStr = '';
                    for (let i = 0; i < notices.length; i++)
                    {
                        noticeStr += '公告' + (i + 1) + ': ' + notices[ i ] + '\n\n';
                    }
                    res.reply (noticeStr);
                }, req);
                break;
            }
            case 'praise' :
            {
                res.reply ('谢谢您的支持!');
                break;
            }
        }
    }
    else if (message.Event === 'LOCATION')
    {
        res.reply ('');
    }
    else
    {
        //res.reply('暂未支持! Coming soon!');
        res.reply ('');
    }
}));